/**
 * Abstract class:	DatabaseManager
 * 
 * Variables:
 * 
 *		private Connection connection;
 *		private Settings settings;
 * 
 * Methods:
 * 
 * 		protected Connection getConnection();
 * 		
 *      protected void connect();
 *      
 *      protected void disconnect();
 *      
 *      protected String addAnd( int notNull );
 *      
 *      protected Date parseDate( java.sql.Date sqlDate );
 * 
 * Remarks:
 * 
 * 		This class should not be touched by the controllers.
 * 
 * @author ( Sharmaine Lim )
 * @version ( 2013-02-28 // YYYY-MM-DD )
 */

package managers;

import java.sql.Connection;
import java.sql.DriverManager;

import preferences.ConnectionSettings;

import models.Date;


public abstract class DatabaseManager {
	
	/* Instance Variables */
	private ConnectionSettings settings;
	private Connection connection;
	
	
	
	/**
	 * Constructors
	 */
	
	public DatabaseManager() {
		connection = null;
		settings = new ConnectionSettings();
		//Settings( "jdbc:mysql://localhost:3306/cosca_ats", "root", "root" );
	}
	
	
	
	/**
	 * Connect
	 *		- creates a connection to the database
	 * 
	 * Parameter
	 *		- n/a
	 *	
	 * Returns
	 *		- n/a
	 */
	protected void connect() {
		try {
			Class.forName( "com.mysql.jdbc.Driver" ).newInstance();
			connection = DriverManager
						 .getConnection( settings.getHostURL(),
								 		 settings.getUser(),
								 		 settings.getPassword() );
		}
		catch( Exception ex ) {
			ex.printStackTrace();
		}
	}
	
	
	
	/**
	 * Disconnect
	 *		- closes the connection to the database
	 * 
	 * Parameter
	 *		- n/a
	 *	
	 * Returns
	 *		- n/a
	 */
	protected void disconnect() {
		try {
			if ( connection != null ) {
				connection.close();
			}
		}
		catch( Exception ex ) {
			ex.printStackTrace();
		}
	}
    
    
    
    /**
     *  Add "AND"
     *  	- for adding ANDs on queries
	 * 
     *  Parameters
     *		- int notNull: for checking if "AND" should be added
     *	
     *	Returns
     *		- String "": notNull is 0
     * 		- String " AND": notNull is greater than 0
     */
    protected String addAnd( int notNull ) {
    	if ( notNull > 0 ) {
    		return " AND";
    	}
    	
    	return "";
    }
    
    
    
    /**
     * Parse Date
     *  	- turns the Date from SQL into something compatible
     *  	  to the models' Date
	 * 
     *  Parameters
     *		- java.sql.Date sqlDate: Date from SQL 
     *	
     *	Returns
     *		- Date: if successful
     *		- null: if unsuccessful
     */
    protected Date parseDate( java.sql.Date sqlDate ) {
		
    	/* Declaration of Local Variables */
		Date date = null;
		char[] cDate = sqlDate.toString().toCharArray();
		int i = 0;
		int year = 0;
		int month = 0;
		int day = 0;
		
		while ( cDate[i] != '-' ) {
			year = year * 10 + cDate[i] - '0';
			i++;
		}
		
		i++;
		
		while ( cDate[i] != '-' ) {
			month = month * 10 + cDate[i] - '0';
			i++;
		}
		
		i++;
		
		while ( i < cDate.length ) {
			day = day * 10 + cDate[i] - '0';
			i++;
		}
		
		date = new Date( year, month, day );
		
		return date;
	}
	
	
	
	/**
	 * Getters
	 */
	
	protected Connection getConnection() {
		return connection;
	}
}
